﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SPA_Prototype.Model
{
    public enum SERVICE_SOURCE_TYPE {NAME,ID};
    public class ServiceManagement
    {
        private DatabaseDataSet _Dataset;
        private DatabaseDataSet.ServiceTableDataTable _DataTable;
        private DatabaseDataSetTableAdapters.ServiceTableTableAdapter _Adt;

        // constructor
        public ServiceManagement(DatabaseDataSet dt)
        {
            _Dataset = dt;
            _DataTable = _Dataset.ServiceTable;
            _Adt = new DatabaseDataSetTableAdapters.ServiceTableTableAdapter();

            LoadDB();

            PriceList = new List<string>();
            DiscountList = new List<string>();
            PercentList = new List<bool>();

            UpdateList();
        }

        // property / field
        public int Count
        {
            get 
            {
                return _DataTable.Rows.Count;
            }
        }
        public List<string> PriceList;
        public List<string> DiscountList;
        public List<bool> PercentList;

        // medthod : update and load
        public AutoCompleteStringCollection GetSourceForService(SERVICE_SOURCE_TYPE type)
        {
           AutoCompleteStringCollection a = new AutoCompleteStringCollection();
            switch (type)
            {
                case SERVICE_SOURCE_TYPE.NAME:
                    for (int j = 0; j < Count; j++) a.Add(_DataTable[j].Name);
                    break;
                case SERVICE_SOURCE_TYPE.ID:
                    for (int j = 0; j < Count; j++) a.Add(_DataTable[j].ID);
                    break;
            }
            return a;
        }

        private void UpdateList()
        {
            PriceList.Clear();
            DiscountList.Clear();
            PercentList.Clear();

            for (int j = 0; j < Count; j++)
            {
                PriceList.Add(_DataTable[j].Price.ToString());
                DiscountList.Add(_DataTable[j].Discount.ToString());
                PercentList.Add(_DataTable[j].Percent);
            }
        }

        public void AddNewService(string serviceID, string serviceName, int price)
        {
            // check whether the ID has already in DataTable
            var query = from q in _DataTable
                        where q.ID == serviceID
                        select new {q.ID};

            foreach (var qu in query)
                if (qu != null)
                {
                    MessageBox.Show("The ID of the service must be unique !", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    return;
                }

            // if the ID of service has not in DataTable, insert new service
            _DataTable.AddServiceTableRow(serviceID, serviceName, price, 0, false);

            UpdateList();

            UpdateDB();
        }

        private void LoadDB()
        {
            _Adt.Fill(_DataTable);
        }

        public void UpdateDB()
        {
            try
            {
                _Adt.Update(_DataTable);
                UpdateList();
                MessageBox.Show("Services update successfully !", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
